package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.info33.platform.business.app.entity.AppAboutUs;
import com.info33.platform.business.app.service.AppAboutUsService;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import com.info33.platform.common.validated.UpdateValidated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppAboutUs")
@Api(tags = "app - 关于我们表")
public class AppAboutUsController extends BaseController<AppAboutUsService, AppAboutUs> {

    private final AppAboutUsService appAboutUsService;

    @Autowired
    public AppAboutUsController(AppAboutUsService appAboutUsService) {
        super(appAboutUsService);
        this.appAboutUsService = appAboutUsService;
    }

    @GetMapping("/app/top")
    @ApiOperation(value = "app 获取")
    public Result<AppAboutUs> aboutUsInfo() {
        return Result.ok(appAboutUsService.getOne(new QueryWrapper<AppAboutUs>()
                .or(l -> l.eq(TableConstant.TENANT_ID, getTenantId())
                        .or().eq("common", Boolean.TRUE))
                .orderByDesc("common")
                .orderByAsc(TableConstant.SORT)
                .orderByDesc(TableConstant.CREATE_DATE)
                .last(TableConstant.LIMIT_ONE)));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "pc 新增")
    public Result<Object> save(
            @RequestBody AppAboutUs entity) {
        entity.setCommon(SysUtil.isSysAdmin());
        return appAboutUsService.save(entity) ? Result.ok() : Result.error();
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "pc 更新")
    public Result<Object> pcUpdate(
            @Validated(value = {UpdateValidated.class}) @RequestBody AppAboutUs entity) {
        return appAboutUsService.pcUpdate(entity);
    }

    @DeleteMapping(value = "/remove/{id}")
    @ApiOperation(value = "pc 删除")
    public Result<Object> pcRemove(
            @PathVariable Long id) {
        return appAboutUsService.pcRemove(id);
    }

}